package com.xiaochen.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochen.manager.service.SysRoleService;
import com.xiaochen.model.dto.system.SysRoleDto;
import com.xiaochen.model.dto.system.SysRoleGetDto;
import com.xiaochen.model.entity.system.SysRole;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户角色管理(权限控制RBAC)
 * TODO: 权限控制
 */
@Slf4j
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 1.条件分页查询角色----方法一
     * Post
     */
    @PostMapping("/findByPage/{page}/{pageSize}")
    public Result findByPage(@RequestBody SysRoleDto sysRoleDto,
                             @PathVariable("page") Integer page,
                             @PathVariable("pageSize") Integer pageSize) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto, page, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 1.条件分页查询角色----方法二
     * Get
     */
    @GetMapping("/findByPageGet")
    public Result findByPageGet(SysRoleGetDto sysRoleGetDto){
        PageInfo<SysRole> sysRolePageInfo = sysRoleService.findByPageGet(sysRoleGetDto);
        return Result.build(sysRolePageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 2.添加角色
     * @param sysRole
     * @return
     */
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        log.info("保存角色信息:{}", sysRole);
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 3.修改角色
     */
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        log.info("修改:{}",sysRole);
        sysRoleService.update(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 4.根据ID进行查询
     */
    @GetMapping("/getById/{roleId}")
    public Result getById(@PathVariable("roleId") Long roleId){
        List<SysRole> sysRoleList = sysRoleService.getById(roleId);
        return Result.build(sysRoleList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 5.根据ID进行删除
     */
    @DeleteMapping("/deleteSysRoleById/{roleId}")
    public Result deleteSysRoleById(@PathVariable("roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 6.查询所有角色
     */
    @GetMapping("/findAllSysRole/{userId}")
    public Result findAllSysRole(@PathVariable Long userId){
        Map<String,Object> sysRoleMap = sysRoleService.findAllSysRole(userId);
        return Result.build(sysRoleMap, ResultCodeEnum.SUCCESS);
    }
}
